package com.liunian.config;

import com.liunian.compress.Compressor;
import com.liunian.compress.CompressorFactory;
import com.liunian.loadbalancer.LoadBalancer;
import com.liunian.serialize.Serializer;
import com.liunian.serialize.SerializerFactory;
import com.liunian.spi.SpiHandler;

import java.util.List;

public class SpiResolver {
    /**
     * 通过spi的方式加载配置项
     * @param configuration 配置上下文
     */
    public void loadFromSpi(Configuration configuration) {
        // 1、spi的文件中配置了很多实现（自由定义，只能配置一个实现，还是多个）
        List<ObjectWrapper<LoadBalancer>> loadBalancerWrappers = SpiHandler.getList(LoadBalancer.class);
        // 将其放入工厂
        if (loadBalancerWrappers != null || loadBalancerWrappers.size() > 0) {
            configuration.setLoadBalancer(loadBalancerWrappers.get(0).getImpl());
        }

        List<ObjectWrapper<Compressor>> compressorWrappers = SpiHandler.getList(Compressor.class);
        if (compressorWrappers != null) {
            compressorWrappers.forEach(CompressorFactory::addCompressor);
        }

        List<ObjectWrapper<Serializer>> serializerObjectWrappers = SpiHandler.getList(Serializer.class);
        if (serializerObjectWrappers != null) {
            serializerObjectWrappers.forEach(SerializerFactory::addSerializer);
        }
    }
}
